package com.stock.demo.framework.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Process;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.stock.demo.framework.ui.TipType;
import com.stock.demo.framework.ui.view.LoadingDialog;
import com.stock.demo.framework.ui.view.TipToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XuZhaoyu on 2017/5/15.
 */

public class BaseActivity extends AppCompatActivity {

    private LoadingDialog mLoadingDialog;
    private TipToast mTipToast;
    public static List<Activity> sActivityArrayList = new ArrayList<>();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sActivityArrayList.add(this);
    }

    public void showTip(TipType type, String text) {
        switch (type) {
            case LOADING:
                if (mLoadingDialog == null) {
                    mLoadingDialog = new LoadingDialog(this);
                }
                mLoadingDialog.setText(text).build();
                break;
            case NONE:
            case ERROR:
            case SUCCESS:
                if (mTipToast == null) {
                    mTipToast = new TipToast(this);
                }
                mTipToast.showToast(type, text);
                break;
        }
    }

    public void endLoading() {
        if (mLoadingDialog == null) {
            return;
        }
        mLoadingDialog.dismiss();
    }

    public void finishAll() {
        for (Activity activity : sActivityArrayList) {
            if (!activity.isFinishing()) {
                activity.finish();
            }
        }
        Process.killProcess(Process.myPid());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sActivityArrayList.remove(this);
    }
}
